package com.easymin.weixin.manager.impl;

import com.easymin.weixin.common.DataForm;
import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.entity.WxAccount;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxAccountManager;
import com.easymin.weixin.service.WxAccountService;
import com.easymin.weixin.service.WxApplicationService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DefaultWxAccountManager implements WxAccountManager {

  @Autowired
  private WxAccountService wxAccountService;

  @Autowired
  private WxApplicationService wxApplicationService;

  @Override
  public DataGrid<Map<String, Object>> findAll(int page, int rows) {

    int offset = (page - 1) * rows;

    RowBounds rowBounds = new RowBounds(offset, rows);
    List<WxAccount> list = wxAccountService.findAll(rowBounds);

    List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < list.size(); i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      WxAccount wxAccount = list.get(i);

      map.put("id", wxAccount.getId());
      map.put("name", wxAccount.getName());
      map.put("token", wxAccount.getToken());
      map.put("appsecret", wxAccount.getAppsecret());
      map.put("wxappId", wxAccount.getWxappId());
      map.put("created", wxAccount.getCreated());
      map.put("ack", wxAccount.getAck());
      map.put("fileName", wxAccount.getFileName());
      map.put("fileContent", wxAccount.getFileContent());
      Long applicationId = wxAccount.getApplicationId();
      WxApplication application = wxApplicationService.fetch(applicationId);
      if (application != null) {
        map.put("address", application.getAddress());
      }
      listMap.add(map);
    }
    long total = wxAccountService.count();
    DataGrid<Map<String, Object>> dataGrid = new DataGrid<Map<String, Object>>();
    dataGrid.setRows(listMap);
    dataGrid.setTotal(total);
    return dataGrid;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxAccount> insert(WxAccount wxAccount) {

    DataForm<WxAccount> dataForm = new DataForm<>();
    WxAccount exists = wxAccountService.exists(wxAccount.getName());
    WxAccount wxAccount2 = wxAccountService.findByFileName(wxAccount.getFileName());
    if (exists == null && wxAccount2==null) {
      wxAccountService.insert(wxAccount);
      dataForm.setStatus(0);
      dataForm.setMessage("成功");
      dataForm.setData(wxAccount);
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("公众号名称或文件名已经存在");
    }

    return dataForm;
  }

  @Override
  public WxAccount fetch(Long id) {

    return wxAccountService.fetch(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxAccount> updateByPrimaryKey(WxAccount wxAccount) {

    DataForm<WxAccount> dataForm = new DataForm<>();

    WxAccount local = wxAccountService.fetch(wxAccount.getId());

    if (null != local) {
      WxAccount exists = wxAccountService.exists(wxAccount.getName());
      if (exists == null || (exists != null && exists.getId().equals(local.getId()))) {
        local.setName(wxAccount.getName());
        local.setApplicationId(wxAccount.getApplicationId());
        local.setToken(wxAccount.getToken());
        local.setWxappId(wxAccount.getWxappId());
        local.setAppsecret(wxAccount.getAppsecret());
        local.setAck(wxAccount.getAck());
        local.setFileName(wxAccount.getFileName());
        local.setFileContent(wxAccount.getFileContent());
        wxAccountService.update(local);
        dataForm.setData(local);
        dataForm.setStatus(0);
        dataForm.setMessage("成功");
      } else {
        dataForm.setStatus(-2);
        dataForm.setMessage("微信公众号名称重复");
      }
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("微信公众号不存在");
    }


    return dataForm;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxAccount> deleteByPrimaryKey(Long id) {

    DataForm<WxAccount> dataForm = new DataForm<>();

    wxAccountService.delete(id);
    dataForm.setStatus(0);
    dataForm.setMessage("成功");

    return dataForm;
  }

  @Override
  public WxAccount findBy(String appid, Long wxApplicationId) {


    return wxAccountService.findBy(appid, wxApplicationId);
  }

  @Override
  public WxAccount findByFileName(String fileName) {
    
    return wxAccountService.findByFileName(fileName);
  }
}
